﻿Imports System.Windows.Input
Imports System.Windows

Public Class DragDropCommand
    Implements ICommand

    Public Property ExecuteMethod As Action(Of Object)


    Private Function CanExecute(ByVal parameter As Object) As Boolean Implements System.Windows.Input.ICommand.CanExecute
        Return True
    End Function

    Private Event CanExecuteChanged(ByVal sender As Object, ByVal e As System.EventArgs) Implements System.Windows.Input.ICommand.CanExecuteChanged

    Private Sub Execute(ByVal parameter As Object) Implements System.Windows.Input.ICommand.Execute

    End Sub
End Class

Public Class DragDropBinder
    Inherits DependencyObject

    'Public Shared Function GetEnableDefault(ByVal element As DependencyObject) As String
    '    If element Is Nothing Then
    '        Throw New ArgumentNullException("element")
    '    End If

    '    Return element.GetValue(EnableDefaultProperty)
    'End Function

    'Public Shared Sub SetEnableDefault(ByVal element As DependencyObject, ByVal value As String)
    '    If element Is Nothing Then
    '        Throw New ArgumentNullException("element")
    '    End If

    '    element.SetValue(EnableDefaultProperty, value)
    'End Sub

    'Public Shared ReadOnly EnableDefaultProperty As  _
    '                       DependencyProperty = DependencyProperty.RegisterAttached("EnableDefault", _
    '                       GetType(String), GetType(Window1), _
    '                       New FrameworkPropertyMetadata(Nothing))

    Public Shared Function GetFormat(ByVal element As DependencyObject) As String
        If element Is Nothing Then
            Throw New ArgumentNullException("element")
        End If

        Return element.GetValue(FormatProperty)
    End Function

    Public Shared Sub SetFormat(ByVal element As DependencyObject, ByVal value As String)
        If element Is Nothing Then
            Throw New ArgumentNullException("element")
        End If

        element.SetValue(FormatProperty, value)
    End Sub

    Public Shared ReadOnly FormatProperty As  _
                           DependencyProperty = DependencyProperty.RegisterAttached("Format", _
                           GetType(String), GetType(DragDropBinder), _
                           New FrameworkPropertyMetadata(Nothing))


    Public Shared Function GetCommand(ByVal element As DependencyObject) As ICommand
        If element Is Nothing Then
            Throw New ArgumentNullException("element")
        End If

        Return element.GetValue(CommandProperty)
    End Function

    Public Shared Sub SetCommand(ByVal element As DependencyObject, ByVal value As ICommand)
        If element Is Nothing Then
            Throw New ArgumentNullException("element")
        End If

        element.SetValue(CommandProperty, value)
    End Sub

    Public Shared ReadOnly CommandProperty As  _
                           DependencyProperty = DependencyProperty.RegisterAttached("Command", _
                           GetType(ICommand), GetType(DragDropBinder), _
                           New FrameworkPropertyMetadata(AddressOf CommandChanged))


    Private Shared Sub CommandChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        If d Is Nothing Then Return
        Dim f As FrameworkElement = d
        If f Is Nothing Then Return

        If e.NewValue Is Nothing Then
            If e.OldValue IsNot Nothing Then
                f.RemoveHandler(DragDrop.DropEvent, New DragEventHandler(AddressOf DropHandler))
            Else

            End If
        Else
            If e.OldValue IsNot Nothing Then
                f.RemoveHandler(DragDrop.DropEvent, New DragEventHandler(AddressOf DropHandler))
            Else
                f.AllowDrop = True
                f.AddHandler(DragDrop.DropEvent, New DragEventHandler(AddressOf DropHandler))
            End If
        End If
    End Sub

    Private Shared Sub DropHandler(ByVal sender As Object, ByVal e As System.Windows.DragEventArgs)
        Dim f As FrameworkElement = TryCast(sender, FrameworkElement)
        If f Is Nothing Then Return
        Dim c = GetCommand(f)
        If c Is Nothing Then Return
        'Dim os As FrameworkElement = TryCast(e.OriginalSource, FrameworkElement)
        'If os IsNot Nothing Then
        '    If os.AllowDrop Then Return
        'End If
        'Dim s As FrameworkElement = TryCast(e.Source, FrameworkElement)
        'If s IsNot Nothing Then
        '    If s.AllowDrop Then Return
        'End If

        'If f IsNot e.Source Then Return
        'Dim data As Object
        'If Not String.IsNullOrEmpty(GetFormat(f)) Then
        '    data = e.Data.GetData(GetFormat(f))
        'Else
        '    data = e.Data.GetData(GetType(Object))
        'End If
        'If c.CanExecute(data) Then
        '    c.Execute(data)
        'End If
    End Sub
End Class
